<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 异常订单脚本
 * @author zxj
 * @description
 */
class ExceptionScript extends CI_Controller {
	function __construct()
    {
        parent::__construct();
        //分析器
        //$this->output->enable_profiler(TRUE);
    }



    /**
     * 未收货超时,发送邮件
     */
    function unfilledOvertime()
    {
        $sql = 'select id,order_num,source_oid,status_id,service_center,point,merchant_num,consumer_email,
        NOW() as now_time,create_time,DATEDIFF(NOW(),create_time) as dayFactor
        from mt_order where status_id=1 and available=1 and unfilled_email=0 and TO_DAYS(NOW())-TO_DAYS(create_time) > 15';
        $orderList = $this->common_model->getQuery($sql,'result_array');
        $merchantArr = array();
        foreach ($orderList as $rs) {
            //散客和商家分发邮件
            if(in_array($rs['point'],array('es','de','ru','fr'))){
                $assign = 'email_unfilled_'.$rs['point'];
            }else{
                $assign = 'email_unfilled_en';
            }
            if(substr($rs['source_oid'],0,2) != 'OM' && filter_var($rs['consumer_email'],FILTER_VALIDATE_EMAIL)){
                gsh_output($rs['order_num'].'发送成功!');
                sendEmail(array('assign'=>'email_unfilled_'.$rs['point'],'emails'=>$rs['consumer_email'],'code'=>$rs['order_num']));
            }else{
                $merchantArr[$rs['merchant_num']][] = $rs;
            }
            //修改是否已发送邮件状态
            $this->common_model->update('mt_order',array('unfilled_email'=>1),array('id'=>$rs['id']));

        }
        //处理商家未发货订单
        foreach ($merchantArr as $key=>$val) {
            //查询平台客户信息
            $merchants = $this->common_model->selectFirst('mt_customers',array('id'=>$key));
            //获取所有订单
            $orderNumbers = implode(',',array_column($merchantArr[$key],'order_num'));
            sendEmail(array('assign'=>'email_unfilled','emails'=>$merchants['email'],'code'=>$orderNumbers));
            //修改是否已发送邮件状态
            gsh_output($orderNumbers.'发送成功!');
            $ids = array_column($merchantArr[$key],'id');
            $this->common_model->update('mt_order',array('unfilled_email'=>1),'',array('id'=>$ids));
        }
    }






}
